package com.project.gjzb.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.gjzb.dto.BusLineStationDto;
import com.project.gjzb.dto.ParamDto;
import com.project.gjzb.entity.BusLineStation;
import com.project.gjzb.entity.TobjBusLine;
import com.project.gjzb.entity.TobjBusLineStation;
import com.project.gjzb.mapper.BusLineStationMapper;
import com.project.gjzb.mapper.TobjBusLineMapper;
import com.project.gjzb.mapper.TobjBusLineStationMapper;
import com.project.gjzb.service.BusDirectionService;
import com.project.gjzb.service.BusLineService;
import com.project.gjzb.service.BusLineStationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class BusLineStationServiceImpl implements BusLineStationService {

    @Resource
    private BusLineStationMapper busLineStationMapper;
    @Resource
    private TobjBusLineStationMapper tobjBusLineStationMapper;
    @Resource
    BusLineService busLineService;

    @Override
    @DS("datacenter")
    public List<TobjBusLineStation> getList() {
        return tobjBusLineStationMapper.getList();
    }

    @Override
    @DS("datacenter")
    public List<TobjBusLineStation> getFirst() {
        return tobjBusLineStationMapper.getFirst();
    }

    @Override
    @DS("datacenter")
    public List<TobjBusLineStation> getMax() {
        return tobjBusLineStationMapper.getMax();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void saveList(List<TobjBusLineStation> list) {
        for (TobjBusLineStation entity : list) {
            tobjBusLineStationMapper.insertSelective(entity);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteByDate(ParamDto paramDto) {
        tobjBusLineStationMapper.deleteByDate(paramDto);
    }

    @Override
    @DS("master")
    public List<BusLineStation> getByDate(String date) {
        Example example = new Example(TobjBusLineStation.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("rq", date);
        c.andEqualTo("isremove", 0);
        List<TobjBusLineStation> list = tobjBusLineStationMapper.selectByExample(example);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<BusLineStation> busLineStationList = new ArrayList<>();
        for (TobjBusLineStation tobjBusLineStation : list) {
            BusLineStation busLineStation = new BusLineStation();
            busLineStation.setId(tobjBusLineStation.getBusstop_id());
            busLineStation.setXlid(tobjBusLineStation.getBusline_id());
            if ("2".equals(tobjBusLineStation.getDirectiontype())) {
                busLineStation.setZxlx("0");
            } else {
                busLineStation.setZxlx(tobjBusLineStation.getDirectiontype());
            }
            if (tobjBusLineStation.getStationlevel() != null) {
                busLineStation.setZj(String.valueOf(tobjBusLineStation.getStationlevel()));
            }
            busLineStation.setZdid(tobjBusLineStation.getStation_id());
            busLineStation.setZdfmc(tobjBusLineStation.getStationname());
            busLineStation.setSbsj(tobjBusLineStation.getStarttime());
            busLineStation.setMbsj(tobjBusLineStation.getEndtime());

            BusLineStationDto busLineStationDto = new BusLineStationDto();
            busLineStationDto.setXlid(tobjBusLineStation.getBusline_id());
            TobjBusLine tobjBusLine = busLineService.getById(busLineStationDto);
            if (tobjBusLine != null) {
                if ("3".equals(tobjBusLine.getOperationtype())) {
                    if(!StringUtils.isEmpty(tobjBusLineStation.getPeakendtime())){
                        busLineStation.setMbsj(tobjBusLineStation.getPeakendtime());
                    }
                    if (!StringUtils.isEmpty(tobjBusLineStation.getEndtime()) && !StringUtils.isEmpty(tobjBusLineStation.getPeakendtime())) {
                        String endTime = tobjBusLineStation.getEndtime();
                        if (tobjBusLineStation.getEndtime().length() < 5) {
                            endTime = "0" + endTime;
                        }
                        String peakendTime = tobjBusLineStation.getPeakendtime();
                        if (tobjBusLineStation.getPeakendtime().length() < 5) {
                            peakendTime = "0" + peakendTime;
                        }
                        endTime = endTime + ":00";
                        peakendTime = peakendTime + ":00";
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
                        LocalTime endTimeT = LocalTime.parse(endTime, formatter);
                        LocalTime peakendTimeT = LocalTime.parse(peakendTime, formatter);
                        if (peakendTimeT.isBefore(endTimeT)) {
                            busLineStation.setMbsj(tobjBusLineStation.getEndtime());
                        }
                    }
                }
            }

            if (tobjBusLineStation.getStationdistance() != null) {
                busLineStation.setZdjl(tobjBusLineStation.getStationdistance());
            }
            busLineStation.setSzfs(tobjBusLineStation.getStationmode());
            busLineStation.setZtlx(tobjBusLineStation.getStationboothtype());
            busLineStation.setDldm(tobjBusLineStation.getRoad_id());
            busLineStation.setXzqdm(tobjBusLineStation.getDistrictcode());
            busLineStation.setHxwz(tobjBusLineStation.getLooplocation());
            if (tobjBusLineStation.getAddtime() != null) {
                busLineStation.setAddtime(sdf.format(tobjBusLineStation.getAddtime()));
            }
            busLineStation.setSzxz("0");
            busLineStationList.add(busLineStation);
        }
        return busLineStationList;
    }

    @Override
    @DS("master")
    public List<BusLineStation> getByXlid(String id, String zxlx) {
        Example example = new Example(BusLineStation.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("xlid", id);
        c.andEqualTo("zxlx", zxlx);
        c.andIsNotNull("zdfmc");
        c.andNotEqualTo("zdfmc", "");
        List<BusLineStation> list = busLineStationMapper.selectByExample(example);
        return list;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteAll() {
        busLineStationMapper.deleteAll();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void insert(List<BusLineStation> list) {
        for (BusLineStation entity : list) {
            busLineStationMapper.insertSelective(entity);
        }
    }

    @Override
    @DS("master")
    public List<BusLineStationDto> getRepeatAll() {
        return busLineStationMapper.getRepeatAll();
    }

    @Override
    @DS("master")
    public List<BusLineStation> getByRepeat(BusLineStationDto dto) {
        return busLineStationMapper.getByRepeat(dto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void deleteById(BusLineStation busLineStation) {
        busLineStationMapper.deleteById(busLineStation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void updateSzmbById(BusLineStation busLineStation) {
        busLineStationMapper.updateSzmbById(busLineStation);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("master")
    public void updateMzsbById(BusLineStation busLineStation) {
        busLineStationMapper.updateMzsbById(busLineStation);
    }

    @Override
    @DS("master")
    public List<BusLineStation> getById(BusLineStation busLineStation) {
        return busLineStationMapper.getById(busLineStation);
    }
}
